﻿#include "DANodeGraphicsView.h"
#include <QMatrix>
#include <QWheelEvent>
#include <QPainter>
#include <QDebug>
DANodeGraphicsView::DANodeGraphicsView(QWidget *parent)
    : QGraphicsView(parent), m_enableShowScenePos(false), m_enaleWheelZoom(true), m_scaleMax(3), m_scaleMin(0.333), m_zoomStep(0.1)
{
    //把鼠标移动事件捕获
    // setMouseTracking(true);
}

DANodeGraphicsView::DANodeGraphicsView(QGraphicsScene *scene, QWidget *parent)
    : QGraphicsView(scene, parent), m_enaleWheelZoom(true), m_scaleMax(3), m_scaleMin(0.333), m_zoomStep(0.1)
{
    // setMouseTracking(true);
}

void DANodeGraphicsView::setScaleRange(qreal min, qreal max)
{
    m_scaleMin = min;
    m_scaleMax = max;
}

qreal DANodeGraphicsView::getScaleMaxFactor() const
{
    return (m_scaleMax);
}

qreal DANodeGraphicsView::getScaleMinFactor() const
{
    return (m_scaleMin);
}

void DANodeGraphicsView::wheelEvent(QWheelEvent *event)
{
    QMatrix old_matrix = matrix();

    if (event->delta() > 0) {
        if ((old_matrix.m11() > m_scaleMax) || (old_matrix.m22() > m_scaleMax)) {
            return;
        }
        old_matrix.scale(1 + m_zoomStep, 1 + m_zoomStep);
        setMatrix(old_matrix);
    } else {
        if ((old_matrix.m11() < m_scaleMin) || (old_matrix.m22() < m_scaleMin)) {
            return;
        }
        old_matrix.scale(1 - m_zoomStep, 1 - m_zoomStep);
        setMatrix(old_matrix);
    }
}

void DANodeGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    if (event) {
        m_mouseScenePos = mapToScene(event->pos());
    }
    QGraphicsView::mouseMoveEvent(event);
}

bool DANodeGraphicsView::isEnableShowScenePos() const
{
    return (m_enableShowScenePos);
}

void DANodeGraphicsView::setEnableShowScenePos(bool enableShowScenePos)
{
    m_enableShowScenePos = enableShowScenePos;
}

bool DANodeGraphicsView::isEnaleWheelZoom() const
{
    return (m_enaleWheelZoom);
}

void DANodeGraphicsView::setEnaleWheelZoom(bool enaleWheelZoom)
{
    m_enaleWheelZoom = enaleWheelZoom;
}
